package com.adams.utils;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class Page<T>
{

	public static final int ALL_SIZE = -1;// 查询所有记录

	public static final int FRONT_SIZE = 4; // 前台展示的页面大小

	public static final int FIRST_PAGE = 1; // 第一页

	public static final int DEFAULT_SIZE = 10; // 每页默认显示10条

	public static final String ASC = "asc";

	public static final String DESC = "desc";

	private int beginCount; // 开始的记录数

	private int totalCount; // 记录总数

	private int pageSize; // 每页显示的记录数

	private int totalPage; // 总页数

	private int currentPage; // 当前页码

	private int prePage;

	private int nextPage;

	private boolean hasPrePage; // 是否有上一页

	private boolean hasNextPage; // 是否有下一页

	private boolean autoCount = true;// 是否计算记录总数

	private String orderBy; // 排序字段

	private String order; // 排序方式

	private String resultContent; // 基于文本的分页

	private List<T> resultList = Collections.emptyList(); // 基于记录的分页

	public Page()
	{

	}

	public static Page<String> getBlankPage(int pageSize)
	{
		Page<String> page = new Page<String>(0, pageSize);
		page.setResultContent("");
		return page;
	}

	public Page(List<T> resultList)
	{
		this.resultList = resultList;
	}

	public Page(int totalCount, int pageSize)
	{
		this(totalCount, pageSize, 1);
	}

	public Page(int totalCount, int pageSize, int currentPage)
	{
		if (pageSize > 0)
		{
			this.pageSize = pageSize;
		} else
		{
			this.pageSize = DEFAULT_SIZE;
		}

		this.totalCount = totalCount;

		if (totalCount <= 0)
		{
			this.totalPage = 1;
		} else
		{
			this.totalPage = (totalCount - 1) / pageSize + 1;
		}

		setCurrentPage(currentPage);
	}

	public Page(List<T> resultList, int totalCount, int pageSize,
			int currentPage)
	{
		this(totalCount, pageSize, currentPage);
		this.resultList = resultList;
	}

	public Page(String resultContent, int totalCount, int pageSize,
			int currentPage)
	{
		this(totalCount, pageSize, currentPage);
		this.resultContent = resultContent;
	}

	public void setCurrentPage(int currentPage)
	{
		if (currentPage <= 0)
		{
			currentPage = FIRST_PAGE;
		}
		this.currentPage = currentPage;
		this.beginCount = (currentPage - 1) * pageSize;

		if (currentPage > 1)
		{
			this.hasPrePage = true;
			this.prePage = currentPage - 1;
		} else
		{
			this.hasPrePage = false;
		}

		if (currentPage < totalPage)
		{
			this.hasNextPage = true;
			this.nextPage = currentPage + 1;
		} else
		{
			this.hasNextPage = false;
		}

	}

	public int getBeginCount()
	{
		return beginCount;
	}

	public int getTotalCount()
	{
		return totalCount;
	}

	@SuppressWarnings("static-access")
	public int getPageSize()
	{
		if (pageSize <= 0 && pageSize != -1)
		{
			pageSize = this.DEFAULT_SIZE;
		}
		return pageSize;
	}

	/**
	 * 
	 * Description : 根据pageSize与totalCount计算总页数
	 * 
	 * @return
	 * 
	 */
	public int getTotalPage()
	{
		if (totalCount < 0)
			totalPage = -1;
		if (pageSize == -1)
		{
			totalPage = 1;
		}
		totalPage = totalCount / pageSize;
		if (totalCount % pageSize > 0)
		{
			totalPage++;
		}

		return totalPage;
	}

	@SuppressWarnings("static-access")
	public int getCurrentPage()
	{
		if (currentPage <= 0)
		{
			currentPage = this.FIRST_PAGE;
		}
		return currentPage;
	}

	/**
	 * 
	 * Description : 是否还有上一页
	 * 
	 * @return
	 * 
	 */
	public boolean isHasPrePage()
	{
		return hasPrePage = (currentPage - 1 >= 1);
	}

	/**
	 * 
	 * Description : 是否还有下一页.
	 * 
	 * @return
	 * 
	 */
	public boolean isHasNextPage()
	{

		return hasNextPage = (currentPage + 1 <= getTotalPage());
	}

	/**
	 * 
	 * Description : 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
	 * 
	 * @return
	 * 
	 */
	public int getPrePage()
	{
		if (isHasPrePage())
			prePage = currentPage - 1;
		else
			prePage = currentPage;
		return prePage;
	}

	/**
	 * 
	 * Description : 取得下页的页号, 序号从1开始 当前页为尾页时仍返回尾页序号.
	 * 
	 * @return
	 * 
	 */
	public int getNextPage()
	{
		if (isHasNextPage())
			nextPage = currentPage + 1;
		else
			nextPage = currentPage;
		return nextPage;
	}

	public List<T> getResultList()
	{
		return resultList;
	}

	public void setResultList(List<T> resultList)
	{
		this.resultList = resultList;
	}

	public String getResultContent()
	{
		return resultContent;
	}

	public void setResultContent(String resultContent)
	{
		this.resultContent = resultContent;
	}

	/**
	 * 
	 * Description : 获得排序字段,无默认值.多个排序字段时用','分隔
	 * 
	 * @return
	 * 
	 */
	public String getOrderBy()
	{
		return orderBy;
	}

	/**
	 * 
	 * Description : 设置排序字段,多个排序字段时用','分隔.
	 * 
	 * @param orderBy
	 * 
	 */
	public void setOrderBy(final String orderBy)
	{
		this.orderBy = orderBy;
	}

	public Page<T> orderBy(final String theOrderBy)
	{
		setOrderBy(theOrderBy);
		return this;
	}

	/**
	 * 
	 * Description : 获得排序方向
	 * 
	 * @return
	 * 
	 */
	public String getOrder()
	{
		return order;
	}

	/**
	 * 
	 * Description : 设置排序方式向
	 * 
	 * @param order
	 *            可选值为desc或asc,多个排序字段时用','分隔.
	 * 
	 */
	public void setOrder(final String order)
	{
		// 检查order字符串的合法值
		String[] orders = StringUtils.split(StringUtils.lowerCase(order), ',');
		for (String orderStr : orders)
		{
			if (!StringUtils.equals(DESC, orderStr)
					&& !StringUtils.equals(ASC, orderStr))
				throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
		}

		this.order = StringUtils.lowerCase(order);
	}

	public Page<T> order(final String theOrder)
	{
		setOrder(theOrder);
		return this;
	}

	/**
	 * 
	 * Description : 是否已设置排序字段,无默认值
	 * 
	 * @return
	 * 
	 */
	public boolean isOrderBySetted()
	{
		return (StringUtils.isNotBlank(orderBy) && StringUtils
				.isNotBlank(order));
	}

	/**
	 * 
	 * Description : 查询对象时是否自动另外执行count查询获取总记录数, 默认为 true
	 * 
	 * @return
	 * 
	 */
	public boolean isAutoCount()
	{
		return autoCount;
	}

	public void setAutoCount(final boolean autoCount)
	{
		this.autoCount = autoCount;
	}

	public Page<T> autoCount(final boolean theAutoCount)
	{
		setAutoCount(theAutoCount);
		return this;
	}

	public void setTotalCount(int totalCount)
	{
		this.totalCount = totalCount;
	}

	public void setBeginCount(int beginCount)
	{
		this.beginCount = beginCount;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}
}
